/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.user.mail;

import hibernate.dao.ConverDAO;
import hibernate.dao.EntityTypeDAO;
import hibernate.dao.MessageDAO;
import hibernate.entity.Conversations;
import hibernate.entity.Entities;
import hibernate.entity.EntityTypes;
import hibernate.entity.MessageConversations;
import hibernate.entity.MessageConversationsId;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import ui.common.AbstractUIBean;
import util.Converter;

/**
 *
 * @author Neo
 */
@ManagedBean
@RequestScoped
public class MailView extends AbstractUIBean {

    /**
     * Creates a new instance of MailView
     */
    Entities currMessage;
    MessageConversations currMC;
    String replyContent;
    Conversations currConver;
    String replyTitle;
    List<MessageConversations> currListMC;
    int maxIndex;
    MessageDAO mesDao;
    boolean replay = false;

    public boolean isReplay() {
        return replay;
    }

    public void setReplay(boolean replay) {
        this.replay = replay;
    }

    public String getReplyTitle() {
        return replyTitle;
    }

    public void setReplyTitle(String replyTitle) {
        this.replyTitle = replyTitle;
    }

    public Entities getCurrMessage() {
        return currMessage;
    }

    public void setCurrMessage(Entities currMessage) {
        this.currMessage = currMessage;
    }

    public MessageConversations getCurrMC() {
        return currMC;
    }

    public String getReplyContent() {
        return replyContent;
    }

    public void setReplyContent(String replyContent) {
        this.replyContent = replyContent;
    }

    public List<MessageConversations> getCurrListMC() {
        Collections.sort(currListMC);
        return currListMC;
    }

    public void setCurrListMC(List<MessageConversations> currListMC) {
        this.currListMC = currListMC;
    }

    public MailView() {
        HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
        currMessage = (Entities) session.getAttribute("currMessage");
        currMC = (MessageConversations) session.getAttribute("currMC");
        replyTitle = (currMessage.getTitle().startsWith("Re:")) ? currMessage.getTitle() : "Re: " + currMessage.getTitle();
        mesDao = new MessageDAO();
        currConver = currMC.getConversations();
        currListMC = mesDao.readByConversation(currConver);
        currListMC.size();
        int i = 0;
        for (MessageConversations mc : currListMC) {
            i++;
        }
        maxIndex = --i;
    }

    public int getMaxIndex() {
        return currListMC.size() - 1;
    }

    public void setMaxIndex(int i) {
        maxIndex = i;
    }

    public String reply() {
        try {
            hbTransaction = hbSession.beginTransaction();
            currConver = currMC.getConversations();
            int order = currMC.getOrderNumber() + 1;
            EntityTypes type = new EntityTypeDAO().readByName("Message");
            Entities ent = new Entities(replyTitle, replyContent, new Date(), new Date(), true);
//            hbSession = getSession();
            hbTransaction = hbSession.beginTransaction();
            ent.setEntityTypes(type);
            hbSession.save(ent);
            MessageConversations mc = new MessageConversations(currConver, currMessage, order);
            //add message and conver to message-conversation
            MessageConversationsId id = new MessageConversationsId(currConver.getConverId(), ent.getEntityId());
            //add sender
            mc.setAccountsBySenderId(getCurrentAccount());
            //add receiver
            mc.setAccountsByReceiverId(currMC.getAccountsBySenderId());
            mc.setOrderNumber(getOrderNumber(currConver) + 1);
            mc.setId(id);
            mc.setReceiverStorageStatus(1);
            mc.setSenderStorageStatus(1);
            mc.setReadStatus(false);

            hbSession.save(mc);
            hbTransaction.commit();
            currListMC.add(mc);
            currListMC = mesDao.readByConversation(currConver);
            if (replay) {
                replay = false;
                return "viewmessage";
            }
            replay = true;
            return "staffviewmessage?faces-redirect=true";
        } catch (Exception e) {
            hbTransaction.rollback();
            return null;
        }

    }
}
